package com.afkt.project.ui.slice;


import com.afkt.project.base.app.BaseActivity;
import com.afkt.project.model.item.TabItem;
import com.afkt.project.slice.UiSlice;
import com.afkt.project.util.LogUtils;
import com.afkt.project.util.SkipUtils;
import com.afkt.project.util.assist.TabListAssist;
import com.ohos.afkt.ResourceTable;
import com.dev.base.widget.BaseButton;
import com.dev.base.widget.BaseTextView;
import com.dev.utils.app.HandlerUtils;
import com.dev.utils.app.ListenerUtils;
import com.dev.utils.app.ResourceUtils;
import com.dev.utils.app.ViewUtils;
import com.dev.utils.app.helper.QuickHelper;
import com.dev.utils.app.helper.ViewHelper;
import com.dev.utils.common.ArrayUtils;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.global.resource.ResourceManager;

import java.util.ArrayList;
import java.util.List;

/**
 * detail: 常见 UI、GradientDrawable 效果等
 * @author Ttt
 */
public class  UIEffectActivity extends BaseActivity
        implements Component.ClickedListener, Component.ComponentStateChangedListener {

    private static final String TAG = "UIEffectActivity";

    // 当前选中的索引
    private int selectTabIndex = -1;

    // Tab Layout 辅助类
    private TabListAssist tabListAssist;

    private ShapeElement shapeElement_selected = new ShapeElement();
    private ShapeElement shapeElement_noSelected = new ShapeElement();
    private ResourceManager resourceManager;
    private List<TabItem> listTabs;

    // ============================
    // = 所有的控件 =
    // ============================
    private DirectionalLayout effect_bar;

    //返回按钮
    private Image effect_return;

    private BaseTextView vidAue10Tv;

    private BaseTextView vidAue11Tv;

    private BaseTextView vidAue20Tv;

    private BaseTextView vidAue21Tv;

    private BaseTextView vidAue30Tv;

    private BaseTextView vidAue31Tv;

    private BaseButton vidAue40Bth;

    private BaseButton vidAue41Bth;

    private DependentLayout five;

    private DependentLayout six;

    private ScrollView vidAue70Scroll;

    private DirectionalLayout vidAue70Linear;

    private TabList vidAue80Tab;

    private TabList vidAue90Tab;


    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_activity_ui_effect;
    }

    @Override
    protected void initView() {
        LogUtils.error(TAG, "initView");
        resourceManager = getResourceManager();
        DirectionalLayout effcet_bar_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_effect_bar);
        effect_bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_effect_bar, null, false);
        effcet_bar_layout.addComponent(effect_bar);
        effect_return = (Image) effect_bar.findComponentById(ResourceTable.Id_effect_return);

        shapeElement_selected.setRgbColor(RgbColor.fromArgbInt(ResourceUtils.getColorForInt(ResourceTable.Color_skyblue)));
        shapeElement_noSelected.setRgbColor(RgbColor.fromArgbInt(ResourceUtils.getColorForInt(ResourceTable.Color_color_f0)));

        vidAue10Tv = (BaseTextView) findComponentById(ResourceTable.Id_vid_aue_1_0_tv);
        vidAue11Tv = (BaseTextView) findComponentById(ResourceTable.Id_vid_aue_1_1_tv);
        vidAue20Tv = (BaseTextView) findComponentById(ResourceTable.Id_vid_aue_2_0_tv);
        vidAue21Tv = (BaseTextView) findComponentById(ResourceTable.Id_vid_aue_2_1_tv);
        vidAue30Tv = (BaseTextView) findComponentById(ResourceTable.Id_vid_aue_3_0_tv);
        vidAue31Tv = (BaseTextView) findComponentById(ResourceTable.Id_vid_aue_3_1_tv);
        vidAue40Bth = (BaseButton) findComponentById(ResourceTable.Id_vid_aue_4_0_Bth);
        vidAue41Bth = (BaseButton) findComponentById(ResourceTable.Id_vid_aue_4_1_Bth);
        five = (DependentLayout) findComponentById(ResourceTable.Id_layout_five);
        six = (DependentLayout) findComponentById(ResourceTable.Id_layout_six);
        vidAue70Linear = (DirectionalLayout) findComponentById(ResourceTable.Id_vid_aue_7_0_linear);
        vidAue70Scroll = (ScrollView) findComponentById(ResourceTable.Id_vid_aue_7_0_scroll);
        vidAue80Tab = (TabList) findComponentById(ResourceTable.Id_vid_aue_8_0_tab);
        vidAue90Tab = (TabList) findComponentById(ResourceTable.Id_vid_aue_9_0_tab);

        changeTab1(vidAue20Tv, vidAue21Tv);
        changeTab2(vidAue31Tv, vidAue30Tv);

        //默认选中
        ViewHelper.get()
                .setSelected(true, vidAue10Tv)
                .setSelected(false, vidAue11Tv);

        if (vidAue10Tv.isSelected()) {
            vidAue10Tv.setBackground(shapeElement_selected);
            vidAue10Tv.setTextColor(ResourceUtils.getColorForColor(ResourceTable.Color_white));
        }
        if (!vidAue11Tv.isSelected()) {
            vidAue11Tv.setBackground(shapeElement_noSelected);
        }

        // ===========
        // = 渐变效果 =
        // ===========

        RgbColor[] colors = new RgbColor[3];
        colors[0] = RgbColor.fromArgbInt(ResourceUtils.getColorForInt(ResourceTable.Color_red));
        colors[1] = RgbColor.fromArgbInt(ResourceUtils.getColorForInt(ResourceTable.Color_blue));
        colors[2] = RgbColor.fromArgbInt(ResourceUtils.getColorForInt(ResourceTable.Color_green));
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setShaderType(ShapeElement.LINEAR_GRADIENT_SHADER_TYPE);
        shapeElement.setGradientOrientation(ShapeElement.Orientation.BOTTOM_RIGHT_TO_TOP_LEFT);
        shapeElement.setRgbColors(colors);
        five.setBackground(shapeElement);

        RgbColor[] colors2 = new RgbColor[3];
        colors2[0] = RgbColor.fromArgbInt(ResourceUtils.getColorForInt(ResourceTable.Color_black));//黑色
        colors2[1] = RgbColor.fromArgbInt(ResourceUtils.getColorForInt(ResourceTable.Color_skyblue)); //天蓝色
        colors2[2] = RgbColor.fromArgbInt(ResourceUtils.getColorForInt(ResourceTable.Color_orange)); //橙色
        ShapeElement shapeElement2 = new ShapeElement();
        shapeElement2.setShaderType(ShapeElement.SWEEP_GRADIENT_SHADER_TYPE);
        shapeElement2.setGradientOrientation(ShapeElement.Orientation.BOTTOM_RIGHT_TO_TOP_LEFT);
        shapeElement2.setRgbColors(colors2);
        six.setBackground(shapeElement2);

        // ============================
        // = HorizontalScrollView Tab =
        // ============================

        // Tab 数据
        listTabs = new ArrayList<>();
        listTabs.add(new TabItem("全部", 0));
        listTabs.add(new TabItem("待付款", 1));
        listTabs.add(new TabItem("待发货", 2));
        listTabs.add(new TabItem("待收货", 3));
        listTabs.add(new TabItem("待评价", 4));
        listTabs.add(new TabItem("已取消", 5));
        listTabs.add(new TabItem("已完成", 6));
        listTabs.add(new TabItem("已关闭", 7));
        listTabs.add(new TabItem("售后", 8));

        for (int i = 0, len = listTabs.size(); i < len; i++) {
            LogUtils.error(TAG, listTabs + "");
            final int position = i;
            TabItem tabItem = listTabs.get(i);

            Component component = QuickHelper.get(new BaseTextView(this))
                    .setText(tabItem.getTitle())
                    .setTextColor(ResourceUtils.getColorForInt(ResourceTable.Color_black))
                    .setPadding(30, 30, 30, 30)
                    .setTextSize(50)
                    .setOnClicks(new Component.ClickedListener() {
                        @Override
                        public void onClick(Component component) {
                            ViewHelper.get()
                                    .setBold(ViewUtils.getChildAt(vidAue70Linear, selectTabIndex), false)
                                    .setTextColor(ViewUtils.getChildAt(vidAue70Linear, selectTabIndex), ResourceUtils.getColorForInt(ResourceTable.Color_black))
                                    .setBold(ViewUtils.getChildAt(vidAue70Linear, position), true)
                                    .setTextColor(ViewUtils.getChildAt(vidAue70Linear, position), ResourceUtils.getColorForInt(ResourceTable.Color_red));
                            // 修改索引
                            selectTabIndex = position;
                            // 滑动 Tab 处理
                            scrollTab(position);
                        }
                    }).getView();
            vidAue70Linear.addComponent(component);
        }
        ViewUtils.getChildAt(vidAue70Linear).simulateClick();


        // =================
        // = TabLayout Tab =
        // =================

        tabListAssist = TabListAssist.get(vidAue80Tab).setFixedMode(false);
        tabListAssist.setListTabs(listTabs).setSelect(tabListAssist.getTabCount() - 1)
                .setTabChangeListener(new TabListAssist.TabChangeListener() {
                    @Override
                    public void onTabChange(TabItem tabItem, int pos) {
                        tabListAssist.setSelect(pos);
                    }
                });

        TabListAssist.get(vidAue90Tab).setFixedMode(true)
                .setListTabs(ArrayUtils.asList(ArrayUtils.subArray(listTabs.toArray(new TabItem[0]), 0, 3)))
                .setSelect(0)
                .setTabChangeListener(new TabListAssist.TabChangeListener() {
                    @Override
                    public void onTabChange(TabItem tabItem, int pos) {
                        LogUtils.error(TAG, "TabItem:" + tabItem.getTitle() + "pos ：" + pos);
                    }
                });

    }

    @Override
    protected void initListener() {
        ListenerUtils.setOnClicks(this,
                effect_return, vidAue10Tv, vidAue11Tv,
                vidAue20Tv, vidAue21Tv, vidAue30Tv,
                vidAue31Tv
        );
        ListenerUtils.setOnStateChangeds(this,
                vidAue10Tv, vidAue11Tv,
                vidAue40Bth, vidAue41Bth
        );
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_effect_return:
                SkipUtils.startActivity(new UiSlice());
                break;
            case ResourceTable.Id_vid_aue_1_0_tv:
                ViewHelper.get()
                        .setSelected(true, vidAue10Tv)
                        .setSelected(false, vidAue11Tv);
                break;
            case ResourceTable.Id_vid_aue_1_1_tv:
                ViewHelper.get()
                        .setSelected(true, vidAue11Tv)
                        .setSelected(false, vidAue10Tv);
                break;
            case ResourceTable.Id_vid_aue_2_0_tv:
                changeTab1(vidAue20Tv, vidAue21Tv);
                break;
            case ResourceTable.Id_vid_aue_2_1_tv:
                changeTab1(vidAue21Tv, vidAue20Tv);
                break;
            case ResourceTable.Id_vid_aue_3_0_tv:
                changeTab2(vidAue30Tv, vidAue31Tv);
                break;
            case ResourceTable.Id_vid_aue_3_1_tv:
                changeTab2(vidAue31Tv, vidAue30Tv);
                break;
        }
    }

    @Override
    public void onComponentStateChanged(Component component, int i) {
        switch (component.getId()) {
            case ResourceTable.Id_vid_aue_1_0_tv:
                if (ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_SELECTED, i)) {
                    vidAue10Tv.setBackground(shapeElement_selected);
                    vidAue10Tv.setTextColor(ResourceUtils.getColorForColor(ResourceTable.Color_white));
                } else {
                    vidAue10Tv.setBackground(shapeElement_noSelected);
                    vidAue10Tv.setTextColor(ResourceUtils.getColorForColor(ResourceTable.Color_black));
                }
                break;
            case ResourceTable.Id_vid_aue_1_1_tv:
                if (ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_SELECTED, i)) {
                    vidAue11Tv.setBackground(shapeElement_selected);
                    vidAue11Tv.setTextColor(ResourceUtils.getColorForColor(ResourceTable.Color_white));
                } else {
                    vidAue11Tv.setBackground(shapeElement_noSelected);
                    vidAue11Tv.setTextColor(ResourceUtils.getColorForColor(ResourceTable.Color_black));
                }
                break;
            case ResourceTable.Id_vid_aue_4_0_Bth:
                if (ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_PRESSED, i)) {
                    LogUtils.error(TAG, "4_0 true");
                    ShapeElement shapeElement = new ShapeElement();
                    shapeElement.setRgbColor(RgbColor.fromArgbInt(ResourceUtils.getColorForInt(ResourceTable.Color_darkorange)));
                    vidAue40Bth.setBackground(shapeElement);
                    vidAue40Bth.setTextColor(ResourceUtils.getColorForColor(ResourceTable.Color_black));
                } else {
                    ShapeElement shapeElement = new ShapeElement();
                    shapeElement.setRgbColor(RgbColor.fromArgbInt(ResourceUtils.getColorForInt(ResourceTable.Color_orange)));
                    vidAue40Bth.setBackground(shapeElement);
                    vidAue40Bth.setTextColor(ResourceUtils.getColorForColor(ResourceTable.Color_white));
                }
                break;
            case ResourceTable.Id_vid_aue_4_1_Bth:
                if (ComponentState.isStateMatched(ComponentState.COMPONENT_STATE_PRESSED, i)) {
                    LogUtils.error(TAG, "4_1 true");
                    ShapeElement shapeElement = new ShapeElement();
                    shapeElement.setRgbColor(RgbColor.fromArgbInt(ResourceUtils.getColorForInt(ResourceTable.Color_blue)));
                    shapeElement.setCornerRadius(20);
                    shapeElement.setStroke(5,RgbColor.fromArgbInt(ResourceUtils.getColorForInt(ResourceTable.Color_black_tran20)));
                    vidAue41Bth.setBackground(shapeElement);
                    vidAue41Bth.setTextColor(ResourceUtils.getColorForColor(ResourceTable.Color_red));
                } else {
                    ShapeElement shapeElement = new ShapeElement();
                    shapeElement.setRgbColor(RgbColor.fromArgbInt(ResourceUtils.getColorForInt(ResourceTable.Color_black)));
                    shapeElement.setCornerRadius(20);
                    shapeElement.setStroke(5,RgbColor.fromArgbInt(ResourceUtils.getColorForInt(ResourceTable.Color_orange)));
                    vidAue41Bth.setBackground(shapeElement);
                    vidAue41Bth.setTextColor(ResourceUtils.getColorForColor(ResourceTable.Color_white));
                }
                break;
        }
    }


    // ===========
    // = 内部方法 =
    // ===========

    /* 切换 Tab
     *
     * @param clickTab   点击 Tab
     * @param unClickTab 未点击 Tab
     */


    private void changeTab1(
            BaseTextView clickTab,
            BaseTextView unClickTab
    ) {

        // 判断点击的是左边还是右边
        if (clickTab.getId() == ResourceTable.Id_vid_aue_2_0_tv) { // 点击左边
            clickTab.setBackground(shapeElement_selected);
            unClickTab.setBackground(shapeElement_noSelected);
        } else {
            clickTab.setBackground(shapeElement_selected);
            unClickTab.setBackground(shapeElement_noSelected);
        }
        // = 字体效果 =
        // 选中变白色
        clickTab.setTextColor(ResourceUtils.getColorForColor(ResourceTable.Color_white));
        // 未选中增加点击效果
        unClickTab.setTextColor(ResourceUtils.getColorForColor(ResourceTable.Color_black));
    }




    /* 切换 Tab
     *
     * @param clickTab   点击 Tab
     * @param unClickTab 未点击 Tab
     */

    private void changeTab2(
            BaseTextView clickTab,
            BaseTextView unClickTab
    ) {
        ShapeElement newSelect = new ShapeElement();
        ShapeElement newNoSelect = new ShapeElement();
        newSelect.setRgbColor(RgbColor.fromArgbInt(ResourceUtils.getColorForColor(ResourceTable.Color_blue).getValue()));
        newNoSelect.setRgbColor(RgbColor.fromArgbInt(ResourceUtils.getColorForColor(ResourceTable.Color_black).getValue()));
        // 判断点击的是左边还是右边
        if (clickTab.getId() == ResourceTable.Id_vid_aue_3_0_tv) { // 点击左边
            // 设置选中颜色
            clickTab.setBackground(newSelect);
            // 设置未选中颜色
            unClickTab.setBackground(newNoSelect);
        } else {
            // 设置选中颜色
            clickTab.setBackground(newSelect);
            // 设置未选中颜色
            unClickTab.setBackground(newNoSelect);
        }
        // = 字体效果 =
        // 选中变白色
        // 设置未选中颜色
        clickTab.setTextColor(ResourceUtils.getColorForColor(ResourceTable.Color_white));
        unClickTab.setTextColor((ResourceUtils.getColorForColor(ResourceTable.Color_red)));
    }

    //获取Color对象
 
    /**
     * 滑动 Tab 处理
     *
     * @param position 需要滑动的索引
     */
    private void scrollTab(final int position) {
        // 延时移动
        HandlerUtils.postRunnable(() -> {
            try {
                int x = 0;
                // 循环遍历
                for (int i = 1; i < position; i++) {
                    try {
                        // 累加宽度
                        x += vidAue70Linear.getComponentAt(i).getWidth();
                    } catch (Exception e) {
                        LogUtils.error(TAG, e + "scrollTab");
                    }
                }
                LogUtils.error(TAG, x + "移动距离");
                // 开始移动位置
                vidAue70Scroll.scrollTo(x, 0);
            } catch (Exception e) {
                LogUtils.error(TAG, e + "scrollTab");
            }
        }, 50);
    }

}
